package com.coolnie.controller;

import com.coolnie.entity.Pager;
import com.coolnie.entity.User;
import com.coolnie.entity.Users;
import com.coolnie.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/6/14 0014.
 */

@Controller
public class UsersController {

    @Autowired
    private UsersService usersService;

    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public String findOne(){

        return "redirect:/users/1";
    }

    /**
     * 获取所有页面信息
     * @param model
     * @param pageIndex
     * @return
     */

    @RequestMapping(value = "/users/{pageIndex}", method = RequestMethod.GET)
    public String index(Model model, @PathVariable int pageIndex){
        Pager<Users> pager = usersService.findAllByPageIndex(pageIndex);

        model.addAttribute("pager", pager);
        return "/users/users";
    }
    @RequestMapping(value = "/{username}/deleted",method = RequestMethod.GET)
    public  String deleted(@PathVariable String username){
        usersService.deleted(username);
//        System.out.println(usersService.deleted(username));
        return "redirect:/users";
    }

    /**
     * 更新用户信息
     * @param username
     * @param model
     * @return
     */
    @RequestMapping(value = "/{username}/update",method = RequestMethod.GET)
    public String update(@PathVariable String username,Model model){
    model.addAttribute("user",usersService.findOne(username));
//    System.out.println(usersService.findOne(username));
    return "users/updateuser";

    }
    @RequestMapping(value ="/update" ,method = RequestMethod.POST)
    public String update(Users user){
        usersService.update(user);
        return "redirect:/users";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(Users users){
//        model.addAttribute("user",usersService.findOne(username));
            return "users/adduser";
        }
        @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addu(Users user){
            usersService.insert(user);
            return "redirect:/users";
        }

    }

